import java.util.HashSet;
import java.util.Set;

public class Solution {
    public static void main(String[] args) {
        int[] nums =  new int[]{0,3,7,2,5,8,4,6,0,1};
    }
    //最长连续序列
    //1.set去重，循环查询
    public int longestConsecutive(int[] nums) {
        int n = nums.length;
        Set<Integer> num_set = new HashSet<>();
        //将元素放入HashSet
        for(int i = 0; i < n; i++) {
            num_set.add(nums[i]);
        }
        int max = 0;
        for(int i : num_set) {
            //如果不存在前一个元素，减少查询时间
            if(!num_set.contains(i-1)) {
                int curNum = i;
                int curMax = 1;
                while(num_set.contains(curNum + 1)) {
                    curNum += 1;
                    curMax += 1;
                }
                max = Math.max(curMax,max);
            }
        }
        return max;
    }
}
